package geektime.spring.springbucks.controller;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/coffee")
public class CoffeeController {
    @Autowired
    private CoffeeService coffeeService;

    @GetMapping(path = "/")
    public ModelAndView showCreateForm() {
        return new ModelAndView("create-order-form");
    }

    @GetMapping("/byXml")
    @ResponseBody
    public List<Coffee> getAllWithParam() {
        return coffeeService.findAllWithParam(1,6);
    }

    @RequestMapping(path = "/byJson/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Coffee getById(@PathVariable Long id){
        Coffee coffee = coffeeService.queryById(id);
        return coffee;
    }

    @RequestMapping(path = "/byXml/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    @ResponseBody
    public Coffee getById2(@PathVariable Long id){
        Coffee coffee = coffeeService.queryById(id);
        return coffee;
    }


}
